#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    vector<int> f(1 << 18);

    vector<bool> vis(19);
    int ma = 0, ans = 1;

    for (int i = 0; i < n; i++) {
        int tma = 0;
        for (int j = 0; j < 18 && i + j < n; j++) {
            if (vis[a[i + j]])  break;
            vis[a[i + j]] = true;
            tma = max(tma, j + 1);

            int now = 0;
            for (int j = 1; j <= 18; j++)
                if (vis[j]) now += (1 << j - 1);

            f[now] = max(tma, f[now]);
        }
        fill(vis.begin(), vis.end(), false);
    }

    int B = (1 << 18) - 1;
    for (int i = 0; i <= B; i++) {
        for (int j = 0; j < 18; j++) {
            if ((1 << j) & i) f[i] = max(f[i], f[i - (1 << j)]);
        }
    }

    for (int i = 0; i <= B; i++) {
        ans = max(ans, f[i] + f[B ^ i]);
    }
    cout << ans;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}